using System;
using System.Collections.Generic;

namespace GenericsFramework.BusinessRules
{
	public abstract class AbstractRuleBuilder<T>
	{
		private readonly IList<IRule<T>> _rules = new List<IRule<T>>();

		protected AbstractRuleBuilder()
		{
			Define();
		}

		protected void Rules(params IRule<T>[] rules)
		{
			foreach (var rule in rules)
				_rules.Add(rule);
		}

		protected abstract void Define();

		protected static Func<T, bool> When(Func<T, bool> when)
		{
			return when;
		}

		protected static IRule<T> Check(Func<T, bool> selector)
		{
			return new BasicRule<T>(selector);
		}

		public bool Validate(T candidate)
		{
			var valid = false;

			foreach (var rule in _rules)
				valid |= rule.IsSatisfiedBy(candidate);

			return valid;
		}
	}
}